package action;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BoardFrontController extends HttpServlet	
	implements Servlet{ 
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doProcess(req,resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doProcess(req,resp);
	}
	void doProcess(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String uri=req.getRequestURI();
		//System.out.println("uri = "+ uri); 
		String contextPath=req.getContextPath();
		String command=uri.substring(contextPath.length());
		System.out.println("command = " + command ); 
		ActionForward forward=null;
		Action action=null;
		
		try{
			if(command.equals("/BoardWrite.bo")){
				forward=new ActionForward();
				forward.setRedirect(false);
				forward.setPath("/board/qna_board_write.jsp");				
			}else if(command.equals("/BoardReplyAction.bo")){
				action = new BoardReplyAction();
				forward=action.execute(req, resp); 
			}else if(command.equals("/BoardDelete.bo")){
				forward=new ActionForward();
				forward.setRedirect(false);
				forward.setPath("/board/qna_board_delete.jsp"); 
			}else if(command.equals("/BoardModify.bo")){
				action = new BoardModifyView();
				forward=action.execute(req, resp); 
			}else if(command.equals("/BoardAddAction.bo")){
				action = new BoardAddAction();
				forward=action.execute(req, resp); 
			}else if(command.equals("/BoardReplyView.bo")){
				action = new BoardReplyView();
				forward=action.execute(req, resp); 
			}else if(command.equals("/BoardModifyAction.bo")){
				action = new BoardModifyAction();
				forward=action.execute(req, resp); 
			}else if(command.equals("/BoardDeleteAction.bo")){
				action = new BoardDeleteAction(); 
				forward=action.execute(req, resp); 
			}else if(command.equals("/BoardList.bo")){
				action = new BoardListAction();
				forward=action.execute(req, resp); 
			}else if(command.equals("/BoardDetailAction.bo")){
				action = new BoardDetailAction();
				forward=action.execute(req, resp); 
			}
			if(forward!=null){
				if(forward.isRedirect()){
					resp.sendRedirect(forward.getPath());
				}else{
					req.getRequestDispatcher(forward.getPath())
						.forward(req, resp);
				}
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

